import functools

# 公用的自定义工具类
from flask import session, current_app, g

from info.models import User


def do_index_class(index):
    """返回指定索引所对应的类名"""

    if index == 0:
        return "first"

    elif index == 1:
        return "second"

    elif index == 2:
        return "third"

    # 如果不是0,1,2，那么就返回空字符串类
    return ""


def user_login_data(f):
    # 使用functools.wraps 装饰内层函数时，会保持当前装饰器去装饰函数的时候__name__的值不变
    @functools.wraps(f)
    def wrapper(*args, **kwargs):
        user_id = session.get("user_id", None)
        user = None
        if user_id:
            # 尝试查询用户的模型
            try:
                user = User.query.get(user_id)
            except Exception as e:
                current_app.logger.error(e)
        # 把查询出来的数据赋值给g变量
        g.user = user
        return f(*args, **kwargs)
    return wrapper



# def query_user_data():
#     # 查询登录用户的信息
#     user_id = session.get('user_id', None)
#     user = None
#
#     if user_id:
#         # 尝试查询用户的模型
#         try:
#             user = User.query.get(user_id)
#         except Exception as e:
#             current_app.logger.error(e)